﻿using System;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;

namespace openxmlconv
{
    public enum DocumentType { OpenXml, OpenDocument };
    public class Document
    {
        // Number of paragraphs inside the document
        public int Count = 0;
        // Paragraphs of the document
        private List<Paragraph> paragraph = new List<Paragraph>();
        // Styles used by paragraphs/runs
        private StyleTable styletable = new StyleTable();

        public override bool Equals(Object obj)
        {            
            Document d = (Document)obj;
            // If they have not the same number of paragrapgs
            // Then they're not equal
            if (Count != d.Count)
            {
                return false;
            }
            // Else each paragraph must be equal to the corresponding
            for (int i = 0; i < Count; i++)
            {
                if (!d[i].Equals(paragraph[i]))
                    return false;
            }
            return true;
        }
        // These 2 methods allows to use "foreach" loops with document
        public Paragraph this[int i]
        {
            get
            {
                return paragraph[i];
            }
        }
        public List<Paragraph>.Enumerator GetEnumerator()
        {
            return paragraph.GetEnumerator();
        }
        // This methods calls the writer class
        public void Write(DocumentType type, string filename)
        {
            switch (type)
            {
                case DocumentType.OpenXml:
                    OpenXmlWriter.Write(this, filename);
                    break;
                case DocumentType.OpenDocument:
                    //OdfWriter.Write(this);
                    break;

            }
        }
        public void Write(DocumentType type)
        {
            switch (type)
            {
                case DocumentType.OpenXml:
                    OpenXmlWriter.Write(this, "nome.docx");
                    break;
                case DocumentType.OpenDocument:
                    //OdfWriter.Write(this);
                    break;
                    
            }
        }

        public static Document Open(string filename)
        {
            ZipFile file;
            try
            {
                file = new ZipFile(filename);
            }
            catch
            {
                throw;
            }
            ZipEntry e = file.GetEntry("content.xml");
            Stream s = file.GetInputStream(e);
            return OdfParser.Parse(s);

        }        
        public StyleTable GetStyleTable()
        {
            return styletable;
        }

        internal void AddParagraph(Paragraph p)
        {
            paragraph.Add(p);
        }

        internal void Add(Run run)
        {
            paragraph[paragraph.Count - 1].Add(run);
        }
    }
}
